package com.esurvey.core;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;


public class Survey {
	private int id;
	private int idOwner;
	private String subject;
	private int idCategory;
	private Date creationDate;
	private Date dueDate;
	private boolean anonymous;
	private SurveyKind kind;
	private ArrayList<Question> questions;
	
	private Survey(int idOwner, String subject, int idCategory, 
			boolean anonymous, ArrayList<Question> questions) {
		this.setIdOwner(idOwner);
		this.setSubject(subject);
		this.setIdCategory(idCategory);
		this.setAnonymous(anonymous);
		this.setQuestions(questions);
		//insertamos, en la propiedad correspondiente, la fecha de creación de la encuesta
		java.util.Calendar fechaActual = new java.util.GregorianCalendar();
		long fechaActualMilisegundos = fechaActual.getTimeInMillis();
		this.creationDate = new Date(fechaActualMilisegundos);
	}
	
	private Survey(int id, int idOwner, String subject, int idCategory,
			boolean anonymous, ArrayList<Question> questions) {
		this(idOwner, subject, idCategory, anonymous, questions);
		this.setId(id);
	}
	
	public Survey(int id, int idOwner, String subject, int idCategory, 
			Date dueDate, boolean anonymous, SurveyKind kind, 
			ArrayList<Question> questions) {
		this(id, idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int id, int idOwner, String subject, int idCategory, 
			String dueDate, boolean anonymous, SurveyKind kind, 
			ArrayList<Question> questions) throws Exception {
		this(id, idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int id, int idOwner, String subject, int idCategory, 
			Date dueDate, boolean anonymous, int kind, 
			ArrayList<Question> questions) throws Exception {
		this(id, idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int id, int idOwner, String subject, int idCategory, 
			String dueDate, boolean anonymous, int kind, 
			ArrayList<Question> questions) throws Exception {
		this(id, idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int idOwner, String subject, int idCategory, 
			Date dueDate, boolean anonymous, SurveyKind kind, 
			ArrayList<Question> questions) {
		this(idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int idOwner, String subject, int idCategory, 
			String dueDate, boolean anonymous, SurveyKind kind, 
			ArrayList<Question> questions) throws Exception {
		this(idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int idOwner, String subject, int idCategory, 
			Date dueDate, boolean anonymous, int kind, 
			ArrayList<Question> questions) throws Exception {
		this(idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int idOwner, String subject, int idCategory, 
			String dueDate, boolean anonymous, int kind, 
			ArrayList<Question> questions) throws Exception {
		this(idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
	}
	
	public Survey(int id, int idOwner, String subject, int idCategory, 
			Date creationDate, Date dueDate, String kind, boolean anonymous,
			ArrayList<Question> questions) throws Exception {
		this(id, idOwner, subject, idCategory, anonymous, questions);
		this.setKind(kind);
		this.setDueDate(dueDate);
		this.setCreationDate(creationDate);
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setIdOwner(int idOwner) {
		this.idOwner = idOwner;
	}

	public int getIdOwner() {
		return idOwner;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSubject() {
		return subject;
	}

	public void setIdCategory(int idCategory) {
		this.idCategory = idCategory;
	}

	public int getIdCategory() {
		return idCategory;
	}
	
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
	public Date getCreationDate() {
		return creationDate;
	}
	
	public String getCreationDateToString() {
		String[] values = creationDate.toString().split("-");
		return values[2]+"/"+values[1]+"/"+values[0];
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}
	
	public void setDueDate(String dueDate) throws Exception {
		String[] values = dueDate.split("/");
		if (values.length == 3) {
		     SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd");
		     String strFecha = values[2] + "-" + values[1] + "-" + values[0];
		     try {
		         this.dueDate = new Date(formatoDelTexto.parse(strFecha).getTime());
		     } catch (Exception e) {
		    	 throw new eSurveyException("Cannot setCreationDate("+dueDate+ "): " + e);
		     }
		} else {
			throw new eSurveyException("Cannot setCreationDate("+dueDate+ "): invalid date");
		}
	}

	public Date getDueDate() {
		return dueDate;
	}
	
	public String getDueDateToString() {
		String[] values = dueDate.toString().split("-");
		return values[2]+"/"+values[1]+"/"+values[0];
	}

	public void setAnonymous(boolean anonymous) {
		this.anonymous = anonymous;
	}

	public boolean isAnonymous() {
		return anonymous;
	}

	public void setKind(SurveyKind kind) {
		this.kind = kind;
	}
	
	public void setKind(int kind) throws Exception {
		switch (kind) {
			case 0:
				this.kind = SurveyKind.PUBLIC;
			break;
			case 1:
				this.kind = SurveyKind.PRIVATE;
			break;
			default:
				throw new eSurveyException("Cannot setKind("+kind+ "): invalid value");
		}
	}
	
	public void setKind(String kind) throws Exception {
		if (kind.toUpperCase().equals("PUBLIC")) {
			this.kind = SurveyKind.PUBLIC;
		} else if (kind.toUpperCase().equals("PRIVATE")) { 
			this.kind = SurveyKind.PRIVATE;
		} else {
			throw new eSurveyException("Cannot setKind("+kind+ "): invalid value");
		}
	}
	
	public SurveyKind getKind() {
		return kind;
	}
	
	public String getKindToString() throws Exception {
		if (kind.equals(SurveyKind.PUBLIC)) {
			return "PÚBLICA";
		} else if (kind.equals(SurveyKind.PRIVATE)) {
			return "PRIVADA";
		} else {
			throw new eSurveyException("Cannot getKindToString(): invalid value "+kind.toString());
		}
	}

	public void setQuestions(ArrayList<Question> questions) {
		this.questions = questions;
	}

	public ArrayList<Question> getQuestions() {
		return questions;
	}
}
